package com.lqgao.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lqgao.entity.*;
import com.lqgao.mapper.AttractionMapper;
import com.lqgao.service.AttractionService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (Attraction)表服务实现类
 *
 * @author makejava
 * @since 2023-06-20 23:31:43
 */
@Service("attractionService")
public class AttractionServiceImpl implements AttractionService {
    @Resource
    private AttractionMapper attractionMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Attraction queryById(Integer id) {
        return attractionMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param attraction  筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public PageResult<Attraction> queryByPage(Attraction attraction, PageRequest pageRequest) {
        PageHelper.startPage(pageRequest.getPageNum() == null ? 1 : pageRequest.getPageNum(), pageRequest.getPageSize() == null ? 10 : pageRequest.getPageSize());
        List<Attraction> attractionList = attractionMapper.queryAll(attraction);
        PageInfo<Attraction> attractionPageInfo = new PageInfo<>(attractionList);
        return new PageResult<>(attractionPageInfo.getTotal(), attractionPageInfo.getList());
    }

    /**
     * 新增数据
     *
     * @param attraction 实例对象
     * @return 实例对象
     */
    @Override
    public Attraction insert(Attraction attraction) {
        int insert = attractionMapper.insert(attraction);
        if (insert <= 0) {
            throw new RuntimeException();
        }
        return attraction;
    }

    /**
     * 修改数据
     *
     * @param attraction 实例对象
     * @return 实例对象
     */
    @Override
    public Attraction update(Attraction attraction) {
        int update = attractionMapper.update(attraction);
        if (update <= 0) {
            throw new RuntimeException();
        }
        return attraction;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     */
    @Override
    public void deleteById(Integer id) {
        int delete = attractionMapper.deleteById(id);
        if (delete <= 0) {
            throw new RuntimeException();
        }
    }

    @Override
    public List<ChartData> getBasicData() {
        List<ChartData> chartDataList = attractionMapper.getBasicData();
        if (chartDataList == null) {
            throw new RuntimeException();
        }
        return chartDataList;
    }

    @Override
    public List<ChartData> getMainData() {
        List<ChartData> chartDataList = attractionMapper.getMainData();
        if (chartDataList == null) {
            throw new RuntimeException();
        }
        return chartDataList;
    }

    @Override
    public List<ChartData> getSubData() {
        List<ChartData> chartDataList = attractionMapper.getSubData();
        if (chartDataList == null) {
            throw new RuntimeException();
        }
        return chartDataList;
    }
}
